package com.apes.fn.scm.dumpComm.model;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import com.apes.scm.masterdata.product.repository.ProductRepository;
import com.apes.scm.masterdata.stock.model.Location;
import com.apes.scm.masterdata.stock.repository.LocationRepository;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Data
public class PublicParam extends DomainService {
    private Product product;
    private ProductUom minFactorUom; // 最小计量单位
    private Location flocation; // 发货仓库
    private Location clocation; // 收货仓库
    private String export; // 调出方
    private String reception; // 调入方
    private String develId; // 仓库级别
    private String statisticsDate; // 统计日期

    private Map<String, Product> productCache = new HashMap();
    private Map<String, Location> locationCache = new HashMap();

    private volatile static PublicParam publicParam = null;

    private PublicParam() {
    }

    public static PublicParam getInstance(String storeLevelId) {
        if (publicParam == null && StringUtils.isEmpty(storeLevelId)) {
            return publicParam = new PublicParam();
        } else if ("000121".equals(storeLevelId)) {
            return publicParam = null;
        } else {
            return publicParam;
        }
    }

    public void setFlocation(String locationId) {
        this.flocation = this.findLocation(locationId);
        this.export = "";
        try {
            export = invoke("scm.transferProductService.accordLCompanyChangeOperation", MapUtil.mapper("id", flocation.getId()));
        } catch (Exception e) {

        }
    }

    public void setClocation(String locationId) {
        this.clocation = this.findLocation(locationId);
        this.reception = "";
        try {
            reception = invoke("scm.transferProductService.accordLCompanyChangeOperation", MapUtil.mapper("id", clocation.getId()));
        } catch (Exception e) {

        }
    }

    private Location findLocation(String locationId) {
        if (StringUtils.isEmpty(this.locationCache.get(locationId))) {
            this.locationCache.put(locationId, SpringManager.getBean(LocationRepository.class).findOne(locationId));
        }
        return this.locationCache.get(locationId);
    }

    public void setProductCache(String productId) {
        if (StringUtils.isEmpty(this.productCache.get(productId))) {
            this.productCache.put(productId, SpringManager.getBean(ProductRepository.class).findOne(productId));
        }
        this.product = this.productCache.get(productId);
    }

}
